﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;

public partial class Business_Booking_SetManagement : WebUI.Page.ManagePage
{
    private SetMO setMO = new SetMO();
    private SetBO setBO = new SetBO();
    private DishBO dBO = new DishBO();
    private DishMO dMO = new DishMO();

    private int pageCount = -1;

    #region 页面加载事件
    /// <summary>
    /// 页面加载事件
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetList();
        }
    }
    #endregion

    #region 删除事件 protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 删除事件
    /// </summary>
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList myArrayList = getIDs(GridView1, "cb_id", "lbl_SetID");
        if (myArrayList != null && myArrayList.Count != 0)
        {
            Delete(myArrayList);
            GetList();
        }
        else
        {
            string str = "请选择要删除的记录！";
            PageError(str, Request.RawUrl);
        }
    }

    #region 根据ID号成批删除 public virtual void DeleteBated(ArrayList myArrayList)
    /// <summary>
    /// 根据ID号成批删除
    /// </summary>
    /// <param name="al">ID号列表</param>
    public void Delete(ArrayList myArrayList)
    {
        int id;
        for (int i = 0; i < myArrayList.Count; i++)
        {
            //获取的包厢ID
            id = Convert.ToInt32(myArrayList[i].ToString());
            //删除本包厢
            if (setBO.Delete(id) > 0)
            {
                PageRight("删除成功！", "SetManagement.aspx", "返回", "");
            }
            else
            {
                PageError("删除失败！", "SetManagement.aspx");
            }
        }
    }
    #endregion

    #endregion

    #region 新增事件  protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 新增事件
    /// </summary>
    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("SetAdd.aspx");
    }
    #endregion

    #region 显示数据 private void GetList()
    /// <summary>
    /// 显示数据
    /// </summary>
    private void GetList()
    {
        DataSet ds = setBO.GetAllList();
        GridView1.DataSource = ds;
        pageCount = ds.Tables[0].Rows.Count;

        if (pageCount > 0)
        {
            ViewState["numbers"] = pageCount;
            GridView1.DataBind();
            PageStatus();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
            ViewState["numbers"] = "0";
            PageStatus();
        }
    }

    private void GetList(string strWhere)
    {
        DataSet ds = setBO.GetList(" SetName like '%" + strWhere + "%' ");
        GridView1.DataSource = ds;
        pageCount = ds.Tables[0].Rows.Count;

        if (pageCount > 0)
        {
            ViewState["numbers"] = pageCount;
            GridView1.DataBind();
            PageStatus();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
            ViewState["numbers"] = "0";
            PageStatus();
        }
    }
    #endregion

    #region 翻页事件
    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        PagerEvent(sender, e, GridView1);
        GetList();
    }
    #endregion

    #region gridview中的事件

    #region PageIndexChanging事件
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    #endregion

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //显示价格
            Label lbl_SetPrice = e.Row.FindControl("lbl_SetPrice") as Label;
            lbl_SetPrice.Text = lbl_SetPrice.Text.ToString().Split('.')[0] + "." +
                 lbl_SetPrice.Text.ToString().Split('.')[1].Substring(0, 2);
            //显示菜式
            Label lbl_SetDish = e.Row.FindControl("lbl_SetDish") as Label;
            string[] strSetDish = lbl_SetDish.Text.Remove(lbl_SetDish.Text.Length - 1, 1).Split(',');
            string dishs = "";
            foreach (string s in strSetDish)
            {
                dMO = dBO.GetModel(Convert.ToInt32(s));
                dishs += dMO.DishName + "+";
            }
            dishs = dishs.EndsWith("+") ? dishs.Remove(dishs.Length - 1, 1) : dishs;
            if (dishs.Length >= 15)
            {
                lbl_SetDish.Text = "<a href='#' style='' title='" + dishs + "'>" + dishs.Substring(0,12) + "...</a>";
            }
            else
            {
                lbl_SetDish.Text = dishs;
            }
        }
        //鼠标在列表行中停留时的样式
        for (int i = -1; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                Label Lbl_RuleReply = (Label)e.Row.FindControl("Lbl_RuleReply");
            }
        }

        //显示行号
        AddNo(GridView1, "lbl_No");
    }

    #region 修改事件
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Edit(sender, e);
    }
    private void Edit(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            Response.Redirect("SetEdit.aspx?SetID=" + e.CommandArgument);
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
    }
    #endregion

    #endregion

    #region 查询事件
    protected void imgbtn_Search_Click(object sender, ImageClickEventArgs e)
    {
        string key = tb_Search.Text.Trim();
        if (!string.IsNullOrEmpty(key))
        {
            GetList(key);
        }
        else
        {
            GetList();
        }
    }
    #endregion

    #region 按钮和翻页事件 void PageStatus()
    private void PageStatus()
    {
        butState(GridView1, btnFirst, btnPrev, btnNext, btnLast);
        ShowStats(GridView1, lblCurrentIndex, lblNumbers, lblPageCount);
    }
    #endregion
    
}
